<template>
  <div>
    <RePage>
      <MyFormCreate :clear-event="getTableList" v-model="searchFrom" :rule="searchFormRules" :key="key">
        <template #field-productId="scope">
          <getProductNoIDTypeSelectList v-model="searchFrom.productId" clearable :placeholder='prompt?.productId'
            :style="{ width: '100%' }" @change="(v) => scope.data.model.callback(v)" @clear="getTableList" />
        </template>
        <template #field-createdAt="scope">
          <el-form-item>
            <el-date-picker type="daterange" range-separator="至" start-placeholder="开始时间" end-placeholder="结束时间"
              value-format="YYYY-MM-DD" placeholder="请选择创建时间" style="width: 100%" v-model="searchFrom.createdAt"
              @change="dateChange" />
          </el-form-item>
        </template>
        <el-button type="primary" @click="DebounceGetTableList">搜索</el-button>
        <sj-button custom-type="sinfo" @click="DebounceResetting">重置</sj-button>
      </MyFormCreate>
     
      <pure-table  :data="tableData.data" :columns="initPureTableConfig(roleTableConfig)" showOverflowTooltip border
        @selection-change="selectionChange" :isShowPag="false">
        <template #useType="{ row }">
          {{ isNullOrEmpty( row.useType == 1 ? '项目' : '个体' ) }}
        </template>
        <template #confirm="{ row }">
          <el-tag :type="CONFIRMSTATUSTEXT[row.confirm]?.type" >
          {{ isNullOrEmpty( CONFIRMSTATUSTEXT[row.confirm]?.label ) }} 
          </el-tag>
          <!-- {{ isNullOrEmpty( CONFIRMSTATUSTEXT[row.confirm].label ) }}  -->

        </template>
        <template #alarmType="{ row }">
         <el-tag :type="ALARMTYPES[row.alarmType]?.type" >
          {{ isNullOrEmpty( ALARMTYPES[row.alarmType]?.label ) }}
          </el-tag>
         

        </template>
        <template #action="{ row }">
          <el-space>
            <sj-link custom-type="sblue" :underline="false" @click="info(row)">查看</sj-link>
          </el-space>
        </template>
      </pure-table>
      <div class="mt-5">
        <el-pagination v-model:current-page="searchFrom.page" v-model:page-size="searchFrom.limit" background
          :layout="PAGE_LAYOUT" :total="tableData.total" @size-change="getTableList" @current-change="getTableList" />
      </div>
    </RePage>
  </div>
</template>
<script lang="ts" setup>
import {isNullOrEmpty,initPureTableConfig} from '@/utils/functions'
import alarminfo from "@/views/operate/alarm-management/alarm-log/alarm-info.vue"
import getProductNoIDTypeSelectList from '@/components/select/dviceProductType/dviceProductTypeSelect.vue'
import MyFormCreate from "@/components/MyFormCreate/index.vue";
import {
  getInfo
} from "@/api/operate/alarm-management/alarm-log/index";
import RePage from "@/components/RePage/index.vue";
import ReTable from "@/components/ReTable/index.vue";
import infos from "./info.vue";
import { reactive, ref, h } from "vue";
import { PAGE_LAYOUT } from "@/constant";
import { Plus, Delete } from "@element-plus/icons-vue";
import { ElMessage, ElMessageBox } from "element-plus";
import {
  gertalarmList, gertalarmDetail
} from "@/api/manage/regulatory-authorities/regulatory-authorities/index";
import { titles, defaultSearchForm, prompt, defaultInfoForm } from "./index";
import { addDialog } from "@/components/ReDialog";
import { ALARMTYPE, ALARMTYPES, CONFIRMSTATUSTEXT, CONFIRMSTATUS, HANDLESTATUS, EVENTLEVEL, HANDLESTATUSTEXT } from "@/constant/alarm-mangenment";
import {
  cloneDeep,
  isAllEmpty,
  deviceDetection,
  debounce
} from "@pureadmin/utils";
import { message } from "@/utils/message";
import { customDebounce, customPromiseDebounce } from "@/utils/functions";
import individuaInfo from './individua-info.vue'
import {  getInfo as individuaInfos, } from '@/api/manage/Individual-management/alarm-log/index';
const searchFrom = ref(defaultSearchForm());
const key = ref(0);
const showInfo = ref();
const formRef = ref();
const tableData = reactive({
  data: [],
  total: 0
});

const searchFormRules = [
  {
    type: "input",
    field: "deviceSN",
    title: titles.deviceSN,
    value: null,
    props: {
      placeholder: prompt.deviceSN,
      clearable: true
    }
  },
  {
    type: "input",
    field: "alarmTitle",
    title: "事件标题",
    value: null,
    props: {
      placeholder: "请输入事件标题",
      clearable: true
    }
  },
  {
    type: "select",
    field: "alarmType",
    title: titles.alarmType,
    options:ALARMTYPE,
    value: null,
    props: {
      placeholder: prompt.alarmType,
      clearable: true
    }
  },
  {
    type: "select",
    field: "confirm",
    title: titles.confirm,
    options:CONFIRMSTATUS,
    value: null,
    props: {
      placeholder: prompt.confirm,
      clearable: true
    }
  },
  {
    type: "fieldComponent",
    field: "productId",
    title: titles.productId,
    value: null,
    props: {
      placeholder: prompt.productId,
      clearable: true
    }
  },
  {
    type: "select",
    field: "alarmLevel",
    title: titles.alarmLevel,
    options:EVENTLEVEL,
    value: null,
    props: {
      placeholder: prompt.alarmLevel,
      clearable: true
    }
  },
  {
    type: "fieldComponent",
    field: "createdAt",
    title: titles.createdAt,
    value: null,
    props: {
      placeholder: '',
      clearable: true
    }
  },
  { type: "number", field: "page", title: "page", hidden: true },
  { type: "number", field: "limit", title: "limit", hidden: true }
];
const selectionData = ref([]);
const roleTableConfig = [
  { prop: "id", label: titles.id, width:80, fixed: 'left' },
  { prop: "unityName", label: titles.unityName, minWidth: 150 },
  { slot: "useType", label: titles.useType, minWidth: 120 },
  { prop: "useTypeName", label: titles.useTypeName, minWidth: 150 },
  { prop: "place", label: titles.place, minWidth: 150 },
  { prop: "productName", label: titles.productName, minWidth: 100 },
  { prop: "deviceSN", label: titles.deviceSN, minWidth: 100 },
  { prop: "alarmTitle", label: titles.alarmTitle, minWidth: 100 },
  { slot: "alarmType", label: titles.alarmType, minWidth: 100 },
  { slot: "confirm", label: titles.confirm, minWidth: 100 },
  { prop: "createdAt", label: titles.createdAt, minWidth: 160 },
  { slot: "action", label: "操作", width: 90, fixed: "right" }
];
const dateChange = (e) => {
  if (e) {
    searchFrom.value.startTime = e[0];
    searchFrom.value.endTime = e[1];
  } else {
    searchFrom.value.startTime = '';
    searchFrom.value.endTime = '';
    getTableList()
  }
}
onMounted(() => {
  getTableList();
});
// 获取列表数据
const getTableList = () => {
  gertalarmList(searchFrom.value).then(res => {
    if (res.code === 200) {
      tableData.data = res.result.data;
      tableData.total = res.result.total;
    }
  });
};
// 重置搜索栏
const resetting = () => {
  //点击重置按键
  Object.assign(searchFrom.value, defaultSearchForm());
  key.value++;
  getTableList();
};
// 详情
const info = (row: any) => {
//  console.log("我是详情啊",row);
 if(row.useType == 1){  //项目详情
  addDialog({
    title: `告警详情`,
    width: "800px",
    draggable: true,
    fullscreen: deviceDetection(),
    fullscreenIcon: true,
    props: {
      deviceId: row.deviceId
    },
    hideFooter: true,
    closeOnClickModal: false,
    contentRenderer: () => h(alarminfo, { ref: formRef }),
    openAutoFocus({ options, index }) {
      options.props = {
        isLoading: true,
        deviceId: row.deviceId,
        formInline: defaultInfoForm()
      };
      getInfo({ id: row.id }).then(res => {
        if (res.code == 200) {
          options.props.formInline = res.result;
        }
        options.props.isLoading = false;
      });
    }
  });
 }else{ //个体详情
  addDialog({
    title: `告警详情`,
    width: "800px",
    fullscreen: deviceDetection(),
    fullscreenIcon: true,
    hideFooter:true,
    closeOnClickModal: false,
    contentRenderer: () => h(individuaInfo, { ref: formRef }),
    openAutoFocus({ options, index }) {
      options.props = {
        isLoading: true,
        formInline: defaultInfoForm()
      }
      individuaInfos({ id: row.id }).then(res => {
        if (res.code == 200) {
          showInfo.value = res.result
          options.props.formInline = res.result
          console.log("详情",options.props.formInline);
          
        }
      }).finally(() => {
        options.props.isLoading = false
      })
    },
  })
 }

};
// 开启弹窗

const selectionChange = data => {
  selectionData.value = data;
};
// 获取列表
const DebounceGetTableList = debounce(getTableList, 1000, true);
// 重置
const DebounceResetting = debounce(resetting, 1000, true);
// 添加请求
// const DebounceAddInfo = customPromiseDebounce(addInfo, 1000, true);
// 编辑请求
// const DebounceEditInfo = customPromiseDebounce(editInfo, 1000, true);
</script>
<style lang="scss" scoped></style>
